#include"gets.h"

int recvfile(int netfd){
    char filename[1024]={0};
    int length;

    recv(netfd,&length,sizeof(int),MSG_WAITALL);
    recv(netfd,filename,length,0);

    int fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    char buf[1024]={0};

    while(1){
        recv(netfd,&length,sizeof(int),MSG_WAITALL);

        if(length == 0){
            break;
        }
        bzero(buf,sizeof(buf));
        recv(netfd,buf,length,MSG_WAITALL);
        write(fd,buf,length);
    }
    close(fd);
    return 0;
}


